# 火车进站问题
def Huochejinzhan(ListWait, ListIn, ListOut):
    if len(ListWait) > 0 and len(ListIn) > 0:
        # 进站
        Huochejinzhan(ListWait[:-1], ListIn + ListWait[-1], ListOut)
        # 出站
        Huochejinzhan(ListWait, ListIn[:-1], ListOut+ListIn[-1])
    elif len(ListWait) > 0 and len(ListIn) == 0:
        # 进站
        Huochejinzhan(ListWait[:-1], ListIn + ListWait[-1], ListOut)
    elif len(ListWait) == 0 and len(ListIn) > 0:
        # 进站
        Huochejinzhan(ListWait, ListIn[:-1], ListOut+ListIn[-1])
    else:
        out.append(ListOut)
out = []
A = int(input())
L1 = input()
L1 = L1.replace(' ', '')[::-1]
L2 = L3 = ''
Huochejinzhan(L1, L2, L3)
# 排序要写好
out = sorted(out)
for i in range(0, len(out)):
    for j in range(0, len(out[i])):
        if j < A - 1:
            print(out[i][j],end=(' '))
        else:
            print(out[i][j])